import requests
import json

API_KEY = "VP1rKWG2zWLLSK4nDvCbIGmV"
SECRET_KEY = "ugEiFi83Or9qIX6HUyHLCQ9a8kLr6P84"


def lexer():
    url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?charset=utf-8&access_token=" + get_access_token()

    payload = json.dumps({
        "text": "Baidu is a high-tech company"
    })
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.json())


def sentiment_classify():
    url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=&access_token=" + get_access_token()

    payload = json.dumps({
        "text": "Baidu is a high-tech company"
    })
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)


def get_access_token():
    """
    使用 AK，SK 生成鉴权签名（Access Token）
    :return: access_token，或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))


def depparser():
    url = "https://aip.baidubce.com/rpc/2.0/nlp/v2/depparser?charset=&access_token=" + get_access_token()

    payload = json.dumps({
        "text": "Baidu is a high-tech company"
    })
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)

